package madixin.t110;

class Solution {

    private boolean result = true;

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        getHeight(root);
        return result;
    }

    private int getHeight(TreeNode node) {
        if (node == null) {
            return 0;
        }

        int left = getHeight(node.left);
        int right = getHeight(node.right);
        if (Math.abs(left - right) > 1) {
            result = false;
        }

        return Math.max(left, right) + 1;
    }
}